*************************************************************************
****************************** Main analyses ****************************
******************************               ****************************
*************************************************************************


***Preparation of the main file
use group_final, clear
set more off
merge m:1 cnt_yr using macro_wide //merge with macro-level data
keep if _merge==3 //only keep matching observations
drop _merge
merge m:1 cnt_yr using cnt //merge with country identification variables

//generate 5-year average for spending variables at the macro level
foreach x in lagedu laghealth lagretire lagunempl lagsoc {
gen `x'_av=(`x'1+`x'2+`x'3+`x'4+`x'5)/5
}

//gen standardized around the mean variables
foreach var of varlist av_party av_income lag* unemp gdp_growth gini {
sum `var'
gen std_`var'=(`var'-r(mean))/r(sd)
}
save multi_level, replace


***Main models
use multi_level, clear
replace cnt=21 if country=="Netherlands" //correct missing value for the Netherlands
egen groupid=group(age degree soclass) //generate group identifiers

**Country random intercepts and group fixed effects with field-specific spending
forvalues k=1/5 {
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagedu`k', i(cnt) mle
estimates store edu`k'
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_laghealth`k', i(cnt) mle
estimates store health`k'
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagretire`k', i(cnt) mle
estimates store retire`k'
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagunempl`k', i(cnt) mle
estimates store unemp`k'
}


**Country random intercepts and group fixed effects with overall social spending
forvalues k=1/5 {
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store edusoc`k'
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store healthsoc`k'
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store retiresoc`k'
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store unempsoc`k'
}

***Less controls
**Country random intercepts and group fixed effects with field-specific spending
forvalues k=1/5 {
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagedu`k', i(cnt) mle
estimates store edu`k'_l
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gdp_growth std_laghealth`k', i(cnt) mle
estimates store health`k'_l
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagretire`k', i(cnt) mle
estimates store retire`k'_l
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagunempl`k', i(cnt) mle
estimates store unemp`k'_l
}


**Country random intercepts and group fixed effects with overall social spending
forvalues k=1/5 {
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store edusoc`k'_l
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store healthsoc`k'_l
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store retiresoc`k'_l
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store unempsoc`k'_l
}


***Without Gini
**Country random intercepts and group fixed effects with field-specific spending
forvalues k=1/5 {
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagedu`k', i(cnt) mle
estimates store edu`k'_wg
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_laghealth`k', i(cnt) mle
estimates store health`k'_wg
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagretire`k', i(cnt) mle
estimates store retire`k'_wg
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagunempl`k', i(cnt) mle
estimates store unemp`k'_wg
}

**Country random intercepts and group fixed effects with overall social spending
forvalues k=1/5 {
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store edusoc`k'_wg
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store healthsoc`k'_wg
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store retiresoc`k'_wg
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store unempsoc`k'_wg
}


***5-year Averages
**All controls
*Country random intercepts and group fixed effects with field-specific spending
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagedu_av, i(cnt) mle
estimates store edu_av
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_laghealth_av, i(cnt) mle
estimates store health_av
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagretire_av, i(cnt) mle
estimates store retire_av
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagunempl_av, i(cnt) mle
estimates store unemp_av

*Country random intercepts and group fixed effects with overall social spending
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store edusoc_av
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store healthsoc_av
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store retiresoc_av
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store unempsoc_av

**Without gini
*Country random intercepts and group fixed effects with field-specific spending
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagedu_av, i(cnt) mle
estimates store edu_av
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_laghealth_av, i(cnt) mle
estimates store health_av
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagretire_av, i(cnt) mle
estimates store retire_av
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagunempl_av, i(cnt) mle
estimates store unemp_av

*Country random intercepts and group fixed effects with overall social spending
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store edusoc_av
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store healthsoc_av
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store retiresoc_av
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_unemp std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store unempsoc_av

**Only gdp growth
*Country random intercepts and group fixed effects with field-specific spending
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagedu_av, i(cnt) mle
estimates store edu_av
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gdp_growth std_laghealth_av, i(cnt) mle
estimates store health_av
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagretire_av, i(cnt) mle
estimates store retire_av
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagunempl_av, i(cnt) mle
estimates store unemp_av

*Country random intercepts and group fixed effects with overall social spending
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store edusoc_av
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store healthsoc_av
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store retiresoc_av
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gdp_growth std_lagsoc_av, i(cnt) mle
estimates store unempsoc_av


***Exclusion of small groups
preserve
drop if size<20

**Country random intercepts and group fixed effects with field-specific spending
forvalues k=1/5 {
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagedu`k', i(cnt) mle
estimates store edu`k'_gr
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_laghealth`k', i(cnt) mle
estimates store health`k'_gr
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagretire`k', i(cnt) mle
estimates store retire`k'_gr
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagunempl`k', i(cnt) mle
estimates store unemp`k'_gr
}

**Country random intercepts and group fixed effects with overall social spending
forvalues k=1/5 {
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store edusoc`k'_gr
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store healthsoc`k'_gr
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store retiresoc`k'_gr
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store unempsoc`k'_gr
}
restore

***Only countries in both waves
preserve
drop if cnt_yr=="DK_2006"|cnt_yr=="ES_2006"|cnt_yr=="FI_2006"|cnt_yr=="GB_2006" ///
|cnt_yr=="JP_2006"|cnt_yr=="NL_2006"|cnt_yr=="PT_2006"|cnt_yr=="SE_2006"

**Country random intercepts and group fixed effects with field-specific spending
forvalues k=1/5 {
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagedu`k', i(cnt) mle
estimates store edu`k'_cnt
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_laghealth`k', i(cnt) mle
estimates store health`k'_cnt
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagretire`k', i(cnt) mle
estimates store retire`k'_cnt
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagunempl`k', i(cnt) mle
estimates store unemp`k'_cnt
}

**Country random intercepts and group fixed effects with overall social spending
forvalues k=1/5 {
xtreg av_spend_edu i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store edusoc`k'_cnt
xtreg av_spend_health i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store healthsoc`k'_cnt
xtreg av_spend_retire i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store retiresoc`k'_cnt
xtreg av_spend_unemplo i.year i.groupid std_av_party std_av_income std_gini std_unemp std_gdp_growth std_lagsoc`k', i(cnt) mle
estimates store unempsoc`k'_cnt
}
restore

.

